package com.xbq.exceleditor.ui

import android.app.AlertDialog
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import com.blankj.utilcode.util.*
import com.gyf.immersionbar.ktx.immersionBar
import com.luck.picture.lib.utils.PictureFileUtils
import com.xbq.exceleditor.R
import com.xbq.exceleditor.bean.viewmodel.MineViewModel
import com.xbq.exceleditor.databinding.FragmentMineBinding
import com.xbq.exceleditor.utils.ensureLoginClick
import com.xbq.exceleditor.utils.ensureVip
import com.xbq.xbqsdk.XbqSdk
import com.xbq.xbqsdk.component.activity.VBFragment
import com.xbq.xbqsdk.core.event.UserInfoChanged
import com.xbq.xbqsdk.core.ext.ensureLogin
import com.xbq.xbqsdk.util.click.onDebouncedClick
import com.xbq.xbqsdk.util.format.FormatUtils
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Author: liaohaiping
 * Time: 2020-10-24
 * Description:
 */
@AndroidEntryPoint
class MineFragment : VBFragment<FragmentMineBinding>() {
    val viewModel: MineViewModel by viewModels()
    override fun useEventBus(): Boolean {
        return true
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.initUserInfo()
        initUserState()
        binding.userContainer.onDebouncedClick {
            if (!viewModel.logined) {
                ensureLogin {
                    initUserState()
                }
            }
        }
        binding.btnAboutUs.onDebouncedClick { ActivityUtils.startActivity(AboutActivity::class.java) }
        binding.btnPrivacy.onDebouncedClick { XbqSdk.startPrivacy(requireContext()) }
        binding.btnBuyVip.onDebouncedClick {
            ensureVip("buyvip") {}
        }
        binding.btnRenewVip.onDebouncedClick {
            ensureVip("buyvip") {}
        }
        binding.btnSetting.ensureLoginClick(requireActivity()) {
            ActivityUtils.startActivity(
                SettingActivity::class.java
            )
        }
        binding.btnUserAgreement.onDebouncedClick { XbqSdk.startUserAgreement(requireContext()) }
        binding.btnClearCache.onDebouncedClick { clearCache() }
        binding.btnFeedback.ensureLoginClick(requireActivity()) {
            ActivityUtils.startActivity(
                FeedbackActivity::class.java
            )
        }

    }

    private fun initUserState() {
        binding.tvUserName.text = viewModel.userName
        binding.tvUserId.text = viewModel.uId
        binding.vipPanel.isVisible = viewModel.ischarge
        binding.btnRenewVip.isVisible = viewModel.vip
        binding.btnBuyVip.isVisible = !viewModel.vip
        binding.tvVipExpireTime.text = viewModel.vipExpireTime
        binding.tvVipExpireTime.isVisible = viewModel.vip
        binding.tvJiesuoMore.isVisible = !viewModel.vip

    }

    override fun onResume() {
        super.onResume()
        immersionBar {
            titleBar(R.id.user_container, true)
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onUserInfoChanged(event: UserInfoChanged) {
        viewModel.initUserInfo()
        //重新绑定数据，不加这一句无法更新界面
        initUserState()
    }


    private fun clearCache() {
        val size1 = FileUtils.getLength(requireContext().cacheDir)
        val size2 = FileUtils.getLength(requireContext().externalCacheDir)
        val size3 = CacheDiskStaticUtils.getCacheCount()
        val size4 = CacheDoubleStaticUtils.getCacheDiskCount()
        val totalCache =
            FormatUtils.formatFileSize((size1 + size2 + size3 + size4).toLong())
        val msg = resources.getString(R.string.confirm_clear_cache).format(totalCache)
        AlertDialog.Builder(context)
            .setMessage(msg)
            .setCancelable(true)
            .setNegativeButton(R.string.cancel) { dialog, which -> dialog.dismiss() }
            .setPositiveButton(R.string.confirm) { dialog, which ->
                dialog.dismiss()
                FileUtils.deleteAllInDir(requireContext().cacheDir)
                FileUtils.deleteAllInDir(requireContext().externalCacheDir)
                CacheDiskStaticUtils.clear()
                CacheDoubleStaticUtils.clear()
                PictureFileUtils.deleteAllCacheDirFile(requireContext())
                ToastUtils.showShort(R.string.cache_was_cleared)
            }
            .show()
    }

}